home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / New System Software Extensions / ASLM SDK v1.1.2 / ASLM Examples / TestTools / Sources / TestTool.cp < prev    next >
Encoding:
Text File  |  1994-11-21  |  11.1 KB  |  471 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TestTool.cp
  3.  
  4.     Contains:    MPW Tool to test out Classes.
  5.  
  6.     Description:
  7.         
  8.         TestTool [-v] [-t] [-c nReps] [className]...
  9.         
  10.         -v             turns verbose mode on (progress messages in MPW window), it is off by default
  11.         -t          turns tracing on (tracing to the Trace Monitor), it is off by default
  12.         -c nReps    nReps must be a positive integer for number of times through the test while loop
  13.         
  14.         
  15.         This test program can be modified to test any tool you develop and do simple stress
  16.         testing by changing what is inside the test while loop.
  17.         
  18.     Caveats:
  19.     
  20.         Command-period will leave some things dangling that will never be disposed.
  21.         
  22.  
  23.     Copyright:    © 1991-1993 by Apple Computer, Inc., all rights reserved.
  24.  
  25. */
  26.  
  27. #ifndef __TYPES__
  28. #include <Types.h>
  29. #endif
  30. #ifndef __TESTTOOL__
  31. #include <TestTool.h>
  32. #endif
  33. #ifdef USEMPW
  34.     #ifndef __MPWSHAREDLIBS__
  35.     #include <MPWSharedLibs.h>
  36.     #endif
  37. #endif
  38. #ifndef __LIBRARYMANAGERCLASSES__
  39. #include <LibraryManagerClasses.h>
  40. #endif
  41. #ifndef __LIBRARYMANAGERUTILITIES__
  42. #include <LibraryManagerUtilities.h>
  43. #endif
  44.  
  45. #ifndef __QUICKDRAW__
  46. #include <QuickDraw.h>
  47. #endif
  48. #ifndef __EVENTS__
  49. #include <Events.h>
  50. #endif
  51. #ifndef __CTYPE__
  52. #include <ctype.h>
  53. #endif
  54. #ifndef __IOSTREAM__
  55. #include <iostream.h>
  56. #endif
  57. #ifndef __STDIO__
  58. #include <stdio.h>
  59. #endif
  60. #ifndef __CURSORCTL__
  61. #include <CursorCtl.h>
  62. #endif
  63.  
  64. static int    getIntArgument(char *arg);
  65. static void    Idle(RgnHandle);
  66. #ifndef USEMPW
  67. static int    myPrintFunc(const char*, char*);
  68. #endif
  69.  
  70. const size_t    kTestPoolSize = 10000;
  71.  
  72. main(int argc, char *argv[]) 
  73. {
  74.  
  75.     int                idx;
  76.     int                argIdx;
  77.     int                theCount        = 1;
  78.     int                numClasses        = 0;
  79.     Boolean            verbose            = false;
  80.     Boolean            trace            = false;
  81.     Boolean            debugging        = false;
  82.     Boolean            nogrowpool        = false;
  83.     Boolean            doAll            = false;
  84.     Boolean            doLoad            = true;
  85.     TPoolNotifier*    savedNotifier = NULL;
  86.     RgnHandle        myRgn;
  87.     char*        classes[50];
  88.     
  89.     InitGraf(&qd.thePort);                // initialize quickdraw so we can use regions
  90.     myRgn = NewRgn();                    // a region to use for WaitNextEvent
  91.  
  92.     InitCursorCtl(NULL);
  93.  
  94. #ifdef USEMPW
  95.     InitLibraryManager(kTestPoolSize, kApplicZone);
  96.     TLibraryManager* testMgr = GetLocalLibraryManager();
  97.     
  98.     if (testMgr == NULL) 
  99.     {
  100.         cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
  101.         return 1;
  102.     }
  103.     //
  104.     // %%% Initialize MPW Library Callbacks
  105.     //
  106.     if (InitStdIOCallbacks() != 0)
  107.     {
  108.         cout << "### ERROR: Could not initialize StdIO callbacks" << endl;
  109.         return 1;
  110.     }
  111. #endif
  112.  
  113.     /*    -----------------------------------------------------------------
  114.         Parse the arguments
  115.         ----------------------------------------------------------------- */
  116.  
  117.     if (argc < 2)
  118.     {
  119.         fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
  120.         fprintf(stderr, "        -v       Verbose\n");
  121.         fprintf(stderr, "        -x       Debugging ON (default is OFF)\n");
  122.         fprintf(stderr, "        -p       Don't grow pool\n");
  123.         fprintf(stderr, "        -t       Trace facility ON (default is OFF\n");
  124.         fprintf(stderr, "        -s       Shutdown the ASLM\n");
  125.         fprintf(stderr, "        -nl    Don't do a Load on the classes\n");
  126.         fprintf(stderr, "        -l       Load the ASLM\n");
  127.         fprintf(stderr, "        -a    Do all tests\n");
  128.         fprintf(stderr, "        -n       <Count> Iteration count\n");
  129.         fprintf(stderr, "        -c       <YourTestTool> the name of a TTestTool subclass to instantiate\n");
  130.         fprintf(stderr, "        -o …  Remaining arguments are passed to <YourTestTool>::InitTest\n");
  131.         return 1;
  132.     }
  133.     
  134.     for (idx = 1; idx < argc && argv[idx][0] == '-' && argv[idx][1] != 'o'; idx++) 
  135.     {
  136.         if (argv[idx][0] == '-') 
  137.         { 
  138.             switch (tolower(argv[idx][1])) 
  139.             {
  140.                 case 'a':
  141.                     if (numClasses != 0)
  142.                     {
  143.                         fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
  144.                         return 1;
  145.                     }
  146.                     doAll = true;
  147.                     break;
  148.                     
  149.                 case 'n': 
  150.                     if (tolower(argv[idx][2]) == 'l')
  151.                     {
  152.                         doLoad = false;
  153.                     }
  154.                     else
  155.                     if (++idx < argc)
  156.                     {
  157.                         theCount = getIntArgument(argv[idx]);
  158.                         if (theCount < 0) 
  159.                         {
  160.                             fprintf(stderr, "### option -n value is not a positive integer: %s\n", 
  161.                                     argv[idx]);
  162.                             return 1;
  163.                         }
  164.                     }
  165.                     else 
  166.                     {
  167.                         fprintf(stderr, "### Not enough arguments\n");
  168.                         return 1;
  169.                     }
  170.                     break;
  171.                     
  172.                 case 'l':
  173.                     if (verbose)
  174.                         cout << "Loading the ASLM" << endl;
  175.                     if (!LoadLibraryManager())
  176.                     {
  177.                         cout << "### ERROR: the ASLM could not be loaded!\n";
  178.                         return 10;
  179.                     }
  180.                     return 0;
  181.                 
  182.                 case 'v':
  183.                     verbose = true;
  184.                     break;
  185.                 
  186.                 case 't':
  187.                     trace = true;
  188.                     break;
  189.                     
  190.                 case 's':
  191.                     if (verbose)
  192.                         cout << "Unloading the ASLM" << endl;
  193.                     UnloadLibraryManager();
  194.                     if (idx+1 < argc && tolower(*(argv[idx+1]+1)) == 'l')
  195.                         if (!LoadLibraryManager())
  196.                         {
  197.                             cout << "### ERROR: the ASLM could not be loaded!\n";
  198.                             return 10;
  199.                         }
  200.                     return 0;
  201.  
  202.                 case 'p':
  203.                     nogrowpool = true;
  204.                     break;
  205.                     
  206.                 case 'x':
  207.                     debugging = true;
  208.                     break;
  209.                     
  210.                 case 'c':
  211.                     if (doAll)
  212.                     {
  213.                         fprintf(stderr, "### -a and -c are mutually exlusive switches!\n");
  214.                         return 1;
  215.                     }
  216.                     idx++;
  217.                     classes[numClasses] = new char[strlen(kTestToolPrefix) +
  218.                                                    strlen(argv[idx]) + 1];
  219.                     strcpy(classes[numClasses], kTestToolPrefix);
  220.                     strcat(classes[numClasses++], argv[idx]);
  221.                     break;
  222.  
  223.                 default:
  224.                     fprintf(stderr, "# Usage - %s [-v] [-x] [-s] [-l] [-n numReps] [-t] [-c ClassName]+ [-o …]\n", argv[0]);
  225.                     fprintf(stderr, "        -v       Verbose\n");
  226.                     fprintf(stderr, "        -x       Debugging ON (default is OFF)\n");
  227.                     fprintf(stderr, "        -p       Don't grow pool\n");
  228.                     fprintf(stderr, "        -t       Trace facility ON (default is OFF\n");
  229.                     fprintf(stderr, "        -s       Shutdown the ASLM\n");
  230.                     fprintf(stderr, "        -l       Load the ASLM\n");
  231.                     fprintf(stderr, "        -a    Do all tests\n");
  232.                     fprintf(stderr, "        -n       <Count> Iteration count\n");
  233.                     fprintf(stderr, "        -c       <YourTestTool> the name of a TTestTool subclass to instantiate\n");
  234.                     fprintf(stderr, "        -o …  Remaining arguments are passed to <YourTestTool>::InitTest\n");
  235.                     return 1;
  236.                 
  237.             }
  238.         }
  239.     }
  240.     
  241.     if (argv[idx][0] == '-' && argv[idx][1] == 'o')
  242.         idx++;
  243.     
  244.     argc -= idx;
  245.     
  246.     argIdx = idx;
  247.     
  248. #ifndef USEMPW
  249.     if (debugging) 
  250.         DebugStr("\pAbout to call InitLibraryManager");
  251.  
  252.     InitLibraryManager(kTestPoolSize, kApplicZone);
  253.     TLibraryManager* testMgr = GetLocalLibraryManager();
  254.     
  255.     if (testMgr == NULL) 
  256.     {
  257.         cout << "### ERROR: Could not allocate a TLibraryManager" << endl;
  258.         return 1;
  259.     }
  260. #endif
  261.     
  262.     if (doAll)
  263.     {
  264.         OSErr    err;
  265.         
  266.         numClasses = 0;
  267.         
  268.         TClassInfo*    info = testMgr->GetClassInfo(ClassID(kTTestToolID), &err);
  269.         if (info == NULL)
  270.         {
  271.             cout << "### ERROR: Could not get a TClassInfo (err = )" << err << endl;
  272.             CleanupLibraryManager();
  273.             return 1;
  274.         }
  275.         char*    str;
  276.         while (str = (char*)info->Next())
  277.         {
  278.             classes[numClasses] = new char[strlen(str) + 1];
  279.             strcpy(classes[numClasses++], str);
  280.         }
  281.         delete info;
  282.     }
  283.     
  284.     
  285.     TStandardPool* thePool = GetLocalPool();
  286.     
  287.     savedNotifier = thePool->GetNotifier();
  288.     if (nogrowpool)
  289.         thePool->SetNotifier(NULL);
  290.         
  291.     PoolInfo    info;
  292.     thePool->GetPoolInfo(info);
  293.     cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  294.     cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  295.     
  296.     // turn trace on or off according to -t option
  297.     if (!trace)
  298.         testMgr->TraceLogOff();
  299.     else
  300.         testMgr->TraceLogOn();
  301.     
  302.     // dump out TLibraryManager info
  303.     testMgr->Dump();
  304.     
  305.     for (idx = 0; idx < numClasses; ++idx)
  306.     {
  307.         TTestTool*    tool = NULL;
  308.         
  309.         Idle(myRgn);
  310.         
  311.         if (verbose)
  312.             cout << "INFO: Creating Tool " << classes[idx] << endl;
  313.             
  314.             
  315.         OSErr theErr;
  316.  
  317.         if (doLoad)
  318.         {
  319.             if (debugging)
  320.                 DebugStr("\pAbout to load class");
  321.             theErr = testMgr->LoadClass(ClassID(classes[idx]), true);
  322.             if (theErr != kNoError)
  323.             {
  324.                 fprintf(stderr, "###LoadClass failed, error = %d\n", theErr);
  325.                 continue;
  326.             }
  327.         }
  328.         else
  329.             if (debugging)
  330.                 DebugStr("\pAbout to new tool");
  331.         // LoadClass succeeded
  332.         tool = (TTestTool*)testMgr->NewObject(ClassID(classes[idx]), ClassID(kTTestToolID), &theErr);
  333.         if (theErr != kNoError)
  334.             fprintf(stderr, "###NewObject Error %d\n", theErr);
  335.         
  336.         if (tool == NULL)
  337.             cout << "### ERROR: Could not create Tool " << classes[idx] << endl;
  338.         else
  339.         {
  340.             tool->SetPool(thePool);
  341.             cout.flush();
  342.     #ifndef USEMPW
  343.             tool->SetPrintf(myPrintFunc);
  344.     #endif
  345.     
  346.             int count = theCount;
  347.             
  348.             if (verbose)
  349.             {
  350.                 PoolInfo    info;
  351.                 thePool->GetPoolInfo(info);
  352.                 cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  353.                 cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  354.             }
  355.             
  356.             tool->InitTest(verbose, debugging, argc, argv+argIdx);
  357.             Idle(myRgn);
  358.             while (count--)
  359.             {
  360.                 tool->RunTestIteration(verbose, debugging);
  361.                 Idle(myRgn);
  362.             }
  363.             tool->EndTest(verbose, debugging);
  364.             fflush(stdout);
  365.             Idle(myRgn);
  366.             
  367.             if (verbose)
  368.                 cout << "INFO: Destroying Tool " << classes[idx] << endl;
  369.             
  370.             if (debugging)
  371.                 DebugStr("\pAbout to destroy tool");
  372.     
  373.             delete tool;
  374.             if (verbose)
  375.             {
  376.                 PoolInfo    info;
  377.                 thePool->GetPoolInfo(info);
  378.                 cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  379.                 cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  380.                 if (nogrowpool)
  381.                 {
  382.                     void* test = thePool->Allocate(kTestPoolSize*2);
  383.                     if (test != NULL)
  384.                     {
  385.                         cout << "ERROR: Allocated block larger than pool size!" << endl;
  386.                         thePool->GetPoolInfo(info);
  387.                         cout << "ERROR: Pool size     = " << info.fFreeBytes << endl;
  388.                         cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
  389.                         thePool->Free(test);
  390.                         thePool->GetPoolInfo(info);
  391.                         cout << "ERROR: Pool size     = " << info.fFreeBytes << endl;
  392.                         cout << "ERROR: Largest Block = " << info.fLargestBlock << endl;
  393.                     }
  394.                     else
  395.                     {
  396.                         thePool->GetPoolInfo(info);
  397.                         cout << "INFO: After freeing chunks -->" << endl;
  398.                         cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  399.                         cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  400.                     }
  401.                 }
  402.             }
  403.             Idle(myRgn);
  404.         }
  405.         
  406.         if (doLoad)
  407.             testMgr->UnloadClass(ClassID(classes[idx]));
  408.     }
  409.  
  410.     if (verbose && numClasses > 1) 
  411.     {
  412.         PoolInfo    info;
  413.         thePool->GetPoolInfo(info);
  414.         cout << "INFO: Pool size     = " << info.fFreeBytes << endl;
  415.         cout << "INFO: Largest Block = " << info.fLargestBlock << endl;
  416.     }
  417.  
  418.     if (nogrowpool)
  419.         thePool->SetNotifier(savedNotifier);
  420.     
  421.     if (verbose) 
  422.         cout << "INFO: Disposing testMgr" << endl;
  423.     CleanupLibraryManager();        // delete the TLibraryManager and its pool
  424.     
  425.     DisposeRgn(myRgn);
  426.  
  427.     return 0;
  428. };
  429.  
  430. /**********************************************************************
  431. ** STATIC Functions
  432. ***********************************************************************/
  433.  
  434. #ifndef USEMPW
  435. static int myPrintFunc(const char* format, char* args)
  436. {
  437.     int    ret;
  438.     ret = vprintf(format, args);
  439.     fflush(stdout);
  440.     return ret;
  441. }
  442. #endif
  443.  
  444. static int getIntArgument(char *arg)
  445. {
  446.     int        theCount    = 0;
  447.     Boolean    sign        = false;
  448.     
  449.     while (*arg == ' ')
  450.         ++arg;
  451.     if (*arg == '-')
  452.     {
  453.         sign = true;
  454.         ++arg;
  455.     }
  456.     else 
  457.         if (*arg == '+')
  458.             ++arg;
  459.         
  460.     while (*arg >= '0' && *arg <= '9')
  461.         theCount = theCount*10 + (*arg++) - '0';
  462.     if (sign)
  463.         theCount = -theCount;
  464.     return theCount;
  465. }
  466.  
  467. static void Idle(RgnHandle myRgn)
  468. {
  469.     EventRecord myEvent;
  470.     WaitNextEvent(nullEvent, &myEvent, 0, myRgn);
  471. }